<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="68px">
            <el-form-item label="乐器" prop="instrumentId">
                <el-select v-model="queryParams.instrumentId" placeholder="请选择乐器" clearable style="width: 240px">
                    <el-option
                        v-for="item in instrumentOptions"
                        :key="item.id"
                        :label="item.name"
                        :value="item.id"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="作曲家" prop="composerId">
                <el-select v-model="queryParams.composerId" placeholder="请选择作曲家" clearable style="width: 240px">
                    <el-option
                        v-for="item in composerOptions"
                        :key="item.id"
                        :label="item.name"
                        :value="item.id"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="合集" prop="collectionId">
                <el-select v-model="queryParams.collectionId" placeholder="请选择合集" clearable style="width: 240px">
                    <el-option
                        v-for="item in collectionOptions"
                        :key="item.id"
                        :label="item.name"
                        :value="item.id"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="出版社" prop="publishId">
                <el-select v-model="queryParams.publishId" placeholder="请选择出版社" clearable style="width: 240px" @change="handleQueryPublishChange">
                    <el-option
                        v-for="item in publishOptions"
                        :key="item.id"
                        :label="item.name"
                        :value="item.id"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="出版社信息" prop="publishInfoId">
                <el-select v-model="queryParams.publishInfoId" placeholder="请选择出版社信息" clearable style="width: 240px">
                    <el-option
                        v-for="item in publishInfoOptions"
                        :key="item.id"
                        :label="item.name"
                        :value="item.id"
                    ></el-option>
                </el-select>
            </el-form-item>
            <!-- <el-form-item label="乐器名称" prop="instrumentName">
                <el-input v-model="queryParams.instrumentName" placeholder="请输入乐器名称" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="作曲家名称" prop="composerName">
                <el-input v-model="queryParams.composerName" placeholder="请输入作曲家名称" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="合集名称" prop="collectionName">
                <el-input v-model="queryParams.collectionName" placeholder="请输入合集名称" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="出版社名称" prop="publishName">
                <el-input v-model="queryParams.publishName" placeholder="请输入出版社名称" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="出版社信息名称" prop="publishInfoName">
                <el-input v-model="queryParams.publishInfoName" placeholder="请输入出版社信息名称" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item> -->
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['system:config:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['system:config:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                    @click="handleDelete" v-hasPermi="['system:config:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['system:config:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="configList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="id" align="center" prop="id" />
            <el-table-column label="乐器名称" align="center" prop="instrumentName" />
            <el-table-column label="作曲家名称" align="center" prop="composerName" />
            <el-table-column label="合集名称" align="center" prop="collectionName" />
            <el-table-column label="出版社名称" align="center" prop="publishName" />
            <el-table-column label="出版社信息名称" align="center" prop="publishInfoName" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['system:config:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['system:config:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

        <!-- 添加或修改配置对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="乐器" prop="instrumentId">
                    <el-select v-model="form.instrumentId" placeholder="请选择乐器" clearable style="width: 100%">
                        <el-option
                            v-for="item in instrumentOptions"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="作曲家" prop="composerId">
                    <el-select v-model="form.composerId" placeholder="请选择作曲家" clearable style="width: 100%">
                        <el-option
                            v-for="item in composerOptions"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="合集" prop="collectionId">
                    <el-select v-model="form.collectionId" placeholder="请选择合集" clearable style="width: 100%">
                        <el-option
                            v-for="item in collectionOptions"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="出版社" prop="publishId">
                    <el-select v-model="form.publishId" placeholder="请选择出版社" clearable style="width: 100%" @change="handlePublishChange">
                        <el-option
                            v-for="item in publishOptions"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="出版社信息" prop="publishInfoId">
                    <el-select v-model="form.publishInfoId" placeholder="请选择出版社信息" clearable style="width: 100%">
                        <el-option
                            v-for="item in publishInfoOptions"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        ></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listConfig, getConfig, delConfig, addConfig, updateConfig } from "@/api/system/config"
import { listCollectionSelect, listComposerSelect, listInstrumentSelect, listPublishSelect, listInfoSelect } from "@/api/system/pdf"

export default {
    name: "Config",
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 配置表格数据
            configList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                instrumentId: null,
                composerId: null,
                collectionId: null,
                publishId: null,
                publishInfoId: null,
                instrumentName: null,
                composerName: null,
                collectionName: null,
                publishName: null,
                publishInfoName: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
            },
            // 乐器下拉框选项
            instrumentOptions: [],
            // 作曲家下拉框选项
            composerOptions: [],
            // 合集下拉框选项
            collectionOptions: [],
            // 出版社下拉框选项
            publishOptions: [],
            // 出版社信息下拉框选项
            publishInfoOptions: []
        }
    },
    created() {
        this.getList();
        this.getInstrumentOptions();
        this.getComposerOptions();
        this.getCollectionOptions();
        this.getPublishOptions();
        // 如果初始有出版社ID，则加载出版社信息
        if (this.queryParams.publishId) {
            this.getPublishInfoOptions(this.queryParams.publishId);
        }
    },
    methods: {
        /** 查询配置列表 */
        getList() {
            this.loading = true
            listConfig(this.queryParams).then(response => {
                console.log(response.rows)
                this.configList = response.rows
                this.total = response.total
                this.loading = false
            })
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                instrumentId: null,
                composerId: null,
                collectionId: null,
                publishId: null,
                publishInfoId: null,
                instrumentName: null,
                composerName: null,
                collectionName: null,
                publishName: null,
                publishInfoName: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null
            }
            this.resetForm("form")
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm")
            this.handleQuery()
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset()
            this.open = true
            this.title = "添加配置"
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset()
            const id = row.id || this.ids
            getConfig(id).then(response => {
                this.form = response.data
                if (this.form.publishId) {
                    this.getPublishInfoOptions(this.form.publishId)
                }
                this.open = true
                this.title = "修改配置"
            })
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateConfig(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功")
                            this.open = false
                            this.getList()
                        })
                    } else {
                        addConfig(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功")
                            this.open = false
                            this.getList()
                        })
                    }
                }
            })
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids
            this.$modal.confirm('是否确认删除配置编号为"' + ids + '"的数据项？').then(function () {
                return delConfig(ids)
            }).then(() => {
                this.getList()
                this.$modal.msgSuccess("删除成功")
            }).catch(() => { })
        },
        /** 获取乐器下拉框数据 */
        getInstrumentOptions() {
            listInstrumentSelect().then(response => {
                this.instrumentOptions = response.data;
            });
        },
        /** 获取作曲家下拉框数据 */
        getComposerOptions() {
            listComposerSelect().then(response => {
                this.composerOptions = response.data;
            });
        },
        /** 获取合集下拉框数据 */
        getCollectionOptions() {
            listCollectionSelect().then(response => {
                this.collectionOptions = response.data;
            });
        },
        /** 获取出版社下拉框数据 */
        getPublishOptions() {
            listPublishSelect().then(response => {
                this.publishOptions = response.data;
            });
        },
        /** 获取出版社信息下拉框数据 */
        getPublishInfoOptions(publishId) {
            if (publishId) {
                listInfoSelect(publishId).then(response => {
                    this.publishInfoOptions = response.data;
                });
            } else {
                this.publishInfoOptions = [];
            }
        },
        /** 出版社选择框变化事件 */
        handlePublishChange(value) {
            this.form.publishInfoId = undefined;
            this.getPublishInfoOptions(value);
        },
        /** 查询表单出版社选择框变化事件 */
        handleQueryPublishChange(value) {
            this.queryParams.publishInfoId = undefined;
            this.getPublishInfoOptions(value);
        },
        
        /** 导出按钮操作 */
        handleExport() {
            this.download('system/config/export', {
                ...this.queryParams
            }, `config_${new Date().getTime()}.xlsx`)
        }
    }
}
</script>
